﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;

namespace docmgr.WinForm
{
    class imageDocument:DocumentBase
    {
        private Image _Image;
        private String _Name;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private String _Code;

        public String Code
        {
            get { return _Code; }
            set { _Code = value; }
        }

        public Image Image
        {
            get { return _Image; }
            set 
            { 
                _Image = value; 
                if(_Image!=null)
                {
                    if (_Image.Size.Width < _Image.Size.Height)
                        DefaultPageSettings.Landscape = true;
                    else
                        DefaultPageSettings.Landscape = false;
                }
            }
        }

        public imageDocument()
        {

        }

        public imageDocument(Image image)
        {
            this.Image = image;

            PaperSize pageSize=new PaperSize();
            pageSize.Width=190;
            pageSize.Height=80;
            DefaultPageSettings.PaperSize = pageSize;

            DefaultPageSettings.Margins = new Margins(10,10,10,10);
        }

        public imageDocument(Image image,String name,String code)
        {
            this.Image = image;
            this.Name = name;
            this.Code = code;
            PaperSize pageSize = new PaperSize();
            pageSize.Width = 190;
            pageSize.Height = 80;
            DefaultPageSettings.PaperSize = pageSize;

            DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
        }
        private  void getPrinter()
        {
            System.Windows.Forms.PrintDialog p = new System.Windows.Forms.PrintDialog();
            p.Document=this;
            System.Windows.Forms.DialogResult dr = p.ShowDialog();

            if(dr==System.Windows.Forms.DialogResult.OK)
            {
                Common.PrinterName = this.PrinterSettings.PrinterName;
            }
        }
        public imageDocument(String name, String code)
        {
            int i = 0;
            while (Common.PrinterName == "")
            {
                getPrinter();//选择打印机
                i++;
                if (i > 4)
                    return;//直接返回。没有选择打印机。就酱紫。
            }
            this.PrinterSettings.PrinterName = Common.PrinterName;

            this.Image = Common.qrImg(code);
            this.Name = name;
            this.Code = code;
            PaperSize pageSize = new PaperSize();
            pageSize.Width = 190;
            pageSize.Height = 80;
            DefaultPageSettings.PaperSize = pageSize;
            DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
        }


        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            int x=Common.xoffset;
            int y = Common.yoffset;
            if (Image == null)
                throw new InvalidOperationException();

            
            Rectangle bestFit = GetBestFitRectangle(e.MarginBounds, Image.Size);

            e.Graphics.DrawImage(Image, 130+x, 15 + y, 50, 50);
            e.Graphics.DrawString(String.Format("海军综计部"), font, brush, 45 + x, 10 + y);
            e.Graphics.DrawString(String.Format("承办人：{0}", Name), font, brush, 45 + x, 35 + y);
            e.Graphics.DrawString(String.Format("{0}", Code), font, brush, 45 + x, 60 + y);
            
        }

        private Rectangle GetBestFitRectangle(Rectangle toContain, Size objectSize)
        {
            bool containerLandScape = true;

            if (toContain.Width < toContain.Height)
                containerLandScape = false;

            float aspecRatio = (float)objectSize.Height / (float)objectSize.Width;
            int midContainerX = toContain.Left + (toContain.Width / 2);
            int midContainerY = toContain.Top + (toContain.Height / 2);

            int x1 = 0, x2 = 0, y1 = 0, y2 = 0;

            if(containerLandScape==false)
            {
                x1 = toContain.Left;
                x2 = toContain.Right;
                int adjustedHeight = (int)((float)toContain.Width * aspecRatio);
                y1 = midContainerY - adjustedHeight / 2;
                y2 = y1 + adjustedHeight;
            }
            else
            {
                y1 = toContain.Top;
                y2 = toContain.Bottom;
                int adjustedWidth = (int)((float)toContain.Height / aspecRatio);
                x1 = midContainerX - adjustedWidth / 2;
                x2 = x1 + adjustedWidth;
            }
            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }
    }
}
